package com.doge.util.aop;

import com.doge.exception.Param402Exception;
import com.doge.util.valid.ParamValidation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/**
 * @program: doge
 * @description:
 * @author: Keyu Li
 * @create: 2021-12-01 20:30
 **/

@Aspect
@Component
public class ParamValidAspect {
//    @Pointcut("execution(* com..UserController.login(..))")
    /*
    * 参考 https://segmentfault.com/q/1010000008418069
    *   实现在 类上注解 和 在方法上 注解同时都可以被拦截
    * */
    @Pointcut("@within(com.doge.util.anno.MarketHibernateValid) " +
            "|| @annotation(com.doge.util.anno.MarketHibernateValid)")
    public void paramPointcut(){}

    @Before("paramPointcut()")
    public void before(JoinPoint join) throws Param402Exception {
        Object[] args = join.getArgs();
        for (Object arg : args) {
            if (arg instanceof BindingResult){
                //TODO: 使用这种方式抛出了 嵌套异常；改用工具类抛出异常
                ParamValidation.validHibernate((BindingResult) arg);
                //String msg = ParamValidation.validHibernate((BindingResult) arg);
                //if (msg==null) return;
                //throw new Param402Exception(msg);
            }
        }
    }
}
